import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useUserStore = defineStore('user', () => {
  const token = ref(localStorage.getItem('token') || '')
  const userRole = ref(localStorage.getItem('userRole') || '')
  const username = ref(localStorage.getItem('username') || '')

  const isLoggedIn = computed(() => !!token.value)
  const isLandlord = computed(() => userRole.value === 'landlord')
  const isAdmin = computed(() => userRole.value === 'admin')

  function setUserInfo(userData) {
    token.value = userData.token
    userRole.value = userData.role
    username.value = userData.username

    localStorage.setItem('token', userData.token)
    localStorage.setItem('userRole', userData.role)
    localStorage.setItem('username', userData.username)
  }

  function clearUserInfo() {
    token.value = ''
    userRole.value = ''
    username.value = ''

    localStorage.removeItem('token')
    localStorage.removeItem('userRole')
    localStorage.removeItem('username')
  }

  return {
    token,
    userRole,
    username,
    isLoggedIn,
    isLandlord,
    isAdmin,
    setUserInfo,
    clearUserInfo
  }
}) 